package com.example.common.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.common.intent.BaseViewIntent
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch


/**
 * 封装ViewModel层
 * */
abstract class BaseViewModel<I : BaseViewIntent> : ViewModel() {
    //定义通道用于收集view层的意图
    val channel : Channel<I> = Channel(Channel.UNLIMITED)
    init {
        viewModelScope.launch {
            //将渠道转换成六并收集view层发送过来的意图
            channel.consumeAsFlow().collect{
                dispatchIntent(it)
            }
        }
    }

    abstract fun dispatchIntent(intent: I)
}